//
//  MyPromoCodeVC.m
//  MyNewProject
//
//  Created by sun on 2019/2/12.
//  Copyright © 2019 sun. All rights reserved.
//

#import "MyPromoCodeVC.h"
#import "XMJSWebViewController.h"

@interface MyPromoCodeVC ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIImageView *qrImageView;
@end

@implementation MyPromoCodeVC
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"invitationCodeBg")];
        [self.view addSubview:_imageView];
    }
    return _imageView;
}
- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = SKFONT_SIZE(15);
        [self.view addSubview:_label];
    }
    return _label;
}
- (UILabel *)textLabel {
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.font = SKFONT_SIZE(14);
        _textLabel.textColor = SKColorWithHex(0x888888);
        _textLabel.numberOfLines = 0;
        NSString *text = @"邀请好友参与竞拍\n即可领取锦鲤缘红包奖励～";
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 10;
        style.alignment = NSTextAlignmentCenter;
        NSMutableAttributedString *attribte = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName:style}];
        _textLabel.attributedText = attribte;
        [self.view addSubview:_textLabel];
    }
    return _textLabel;
}
- (UIImageView *)qrImageView {
    if (!_qrImageView) {
        _qrImageView = [[UIImageView alloc] init];
        [self.view addSubview:_qrImageView];
    }
    return _qrImageView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"我的推广码";
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(self.imageView.mas_width).multipliedBy(202.5/159);
    }];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.mas_equalTo(self.imageView.mas_top).mas_offset(-20);
    }];
    [self.qrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imageView.mas_left).mas_offset(30);
        make.right.mas_equalTo(self.imageView.mas_right).mas_offset(-30);
        make.top.mas_equalTo(self.imageView.mas_top).mas_offset(30);
        make.height.mas_equalTo(self.qrImageView.mas_width);
    }];
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.imageView);
        make.bottom.mas_equalTo(self.imageView.mas_bottom).mas_offset(-10);
        make.top.mas_equalTo(self.qrImageView.mas_bottom).mas_offset(10);
    }];
    [self getPicAndMa];
}
-(void)getPicAndMa {
    SKWeakSelf;
    [YQNetworking postWithUrl:@"my/getShare" params:@{@"uid":self.uid,@"loginKey":self.loginKey,@"type":@"ios"} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        if ([response[@"status"] isEqualToString:@"ok"]) {
            [SVProgressHUD show];
            weakSelf.label.text = [NSString stringWithFormat:@"我的邀请码：%@", SafeString([UserInfoSingleton shareInstance].userInfo.reg_num)];
            NSString * url = response[@"data"];
            [weakSelf.qrImageView sd_setImageWithURL:[NSURL URLWithString:SafeString(url)] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                if (weakSelf.qrImageView.image) {
                    
                }else {
                    [weakSelf.view makeToast:@"图片加载失败" duration:1 position:CSToastPositionCenter];
                }
                [SVProgressHUD dismiss];
            }];
        }
        else {
            [weakSelf.view makeToast:SafeString(response[@"msg"]) duration:1 position:CSToastPositionCenter];
        }
    } failBlock:^(NSError *error) {
        [weakSelf.view makeToast:@"请求失败，请稍后重试" duration:1 position:CSToastPositionCenter];
        [SVProgressHUD dismiss];
    }];
}

@end
